﻿using UnityEngine;
using System.Collections;

public class FadeAfterShotDestroyed : MonoBehaviour, IResettable {

    public float fadeRate = 1.0f;   // Reduce trail time by this much per second

    private TrailRenderer trailRenderer;
    private float fadeTime = 0.0f;
    private bool fadeStarted = false;

	// Use this for initialization
	void Start () {
        trailRenderer = GetComponent<TrailRenderer>();
        ResetItemList.Instance.AddItem(this);
	}
	
	// Update is called once per frame
	void Update () {

        if (fadeStarted)
        {
            fadeTime += Time.deltaTime;
            if (fadeTime > trailRenderer.time)
            {
                ResetItemList.Instance.RemoveItem(this);
                Destroy(this.gameObject);
            }
        }

	}

    public void StartFade(float timeAlive)
    {
        fadeStarted = true;
        if (timeAlive < trailRenderer.time)
        {
            trailRenderer.time = timeAlive;
        }
    }

    public void Reset()
    {
        Destroy(this.gameObject);
    }

    public bool PersistAfterReset()
    {
        return false;
    }
}
